{% extends base.html %}
{% block body %}
    <div class="container-fluid">
        <!-- <div class="row">
            <div class="col-xs-2 p0">
                <h2>Console</h2>
            </div>
            <div class="col-xs-10 p0">
                <h2>Result</h2>
            </div>
        </div> -->

        <div class="row">

            <div class="col-xs-2 p0 bc-gray">
                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <ul class="nav" id="side-menu">
                            <li>
                                <a href="/">
                                    <i class="fa fa-bug fa-fw"></i> Home
                                </a>
                            </li>

                            <li>
                                <a href="/tasks">
                                    <i class="fa fa-list-ul fa-fw"></i> Result
                                </a>
                            </li>

                            <li>
                                <a href="/config" class="active">
                                    <i class="fa fa-cog fa-fw"></i> Config
                                </a>
                            </li>
                            <li>
                                <a href="/exit" >
                                    <i class="fa fa-exit fa-fw"></i>Exit</a>
                            </li>

                            <!-- <li>
                                <a href="javascript:;">
                                    <i class="fa fa-bar-chart-o fa-fw"></i> None
                                    <span class="fa arrow"></span>
                                </a>
                                <ul class="nav nav-second-level">
                                    <li><a href="{:U('Baidu/index')}">百度音乐</a></li>
                                    <li><a href="{:U('Qq/index')}">QQ音乐</a></li>
                                    <li><a href="{:U('Netease/index')}">网易云音乐</a></li>
                                </ul>
                            </li> -->
                        </ul>
                    </div>
                </div>
            </div>

            <div class="col-xs-10">
                <div class="row">
                    <div class="col-lg-12">
                        <h3 class="page-header">Config</h3>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>

                <div class="row">
                    <div class="col-xs-12">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <div class="row">
                                    <div class="col-xs-4">
                                    白名单
                                    </div>
                                    <div class="col-xs-8 text-right">
                                        <button class="btn btn-xs btn-info">
                                            <i class="fa fa-refresh"></i> 刷新
                                        </button>
                                    </div>
                                </div>
                            </div>

                            <table class="table table-hover table-bordered table-trust">
                                <thead>
                                    <tr>
                                        <th class="text-center w50">
                                            <input type="checkbox" id="all-checkbox">
                                        </th>
                                        <th>
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="URL - 白名单">
                                                URL
                                            </span>
                                        </th>
                                        <th class="text-center w80">
                                            <span class="" 
                                                  data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="Status - 状态值">
                                                Status
                                            </span>
                                        </th>
                                        <th class="w150">
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="Operating - 相关操作">
                                                Operating
                                            </span>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                {% for white in whites %}
                                    <tr>
                                        <td class="text-center"><input type="checkbox" value="{{ white['host']}}"></td>
                                        <td>
                                            <a href="http://{{ white['host']}}" 
                                                data-toggle="tooltip" 
                                                data-placement="bottom" 
                                                title="{{ white['host']}}" 
                                                target="_black">
                                                {{ white['host']}}
                                            </a>
                                        </td>
                                        <td class="text-center">
                                            <i class="status status-success"
                                                data-toggle="tooltip" 
                                                data-placement="right" 
                                                title="Start" 
                                                onclick="reStart($(this), 0)"></i>
                                        </td>
                                        <td class="wmax80">
                                            <a href="javascript:;" class="btn-xs btn-link btn-delete" onclick="removeItem($(this))">Delete</a>
                                        </td>
                                    </tr>
                                    {% end %}
                                    <!-- <tr>
                                        <td class="text-center"><input type="checkbox"></td>
                                        <td>
                                            <a href="http://www.baidu.com" 
                                                data-toggle="tooltip" 
                                                data-placement="bottom" 
                                                title="http://www.baidu.com"
                                                target="_black">
                                                http://www.baidu.com
                                            </a>
                                        </td>
                                        <td class="text-center">
                                            <i class="status status-error" 
                                                data-toggle="tooltip" 
                                                data-placement="right" 
                                                title="Close" 
                                                onclick="reStart($(this), 0)"></i>
                                        </td>
                                        <td class="wmax80">
                                            <a href="javascript:;" class="btn-xs btn-link btn-delete" onclick="removeItem($(this))">Delete</a>
                                        </td>
                                    </tr> -->
                                </tbody>
                            </table>
                            
                            <div class="panel-footer">
                                <div class="row">
                                    <div class="col-xs-4">
                                        <button type="button" 
                                            class="btn btn-xs btn-default" 
                                            id="btn-removeAll" 
                                            >
                                            批量删除
                                        </button>
                                    </div>
                                    <div class="col-xs-8 text-right">
                                        <button class="btn btn-xs btn-default" onclick="addModal( $(this) )">
                                            <i class="fa fa-plus"></i> 添加
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-12">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                UserAgent
                            </div>
                            <div class="panel-body">
                                <textarea class="form-control">Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36</textarea>
                            </div>
                            <div class="panel-footer">
                                <button class="btn btn-xs btn-default">
                                    <i class="fa fa-save"></i> Save
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Add Modal -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">Detail</h4>
                </div>

                <div class="modal-body">
                    <div class="row">
                        <form >
                            <div class="form-group">
                                <label class="col-xs-1 control-label text-right">
                                    <b class="text-right">URL</b>
                                </label>
                                <div class="col-xs-11">
                                    <input type="text" value="" id="host" name="host" class="form-control input-sm">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-xs" data-dismiss="modal">
                        Close
                    </button>
                    <button type="button" class="btn btn-info btn-xs" onclick="addUrl()">
                        Add URL
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- jQuery -->
    <script src="{{ static_url('js/jquery.min.js') }}"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="{{ static_url('js/bootstrap.min.js') }}"></script>

    <!-- MetisMenu Plugin Js -->
    <script src="{{ 'http://cdn.bootcss.com/metisMenu/2.2.0/metisMenu.min.js' }}"></script>
    <!-- Base JavaScript -->
    <script src="{{ static_url('js/base.js') }}"></script>

    <script type="text/javascript">
        $(document).ready(function(){

            $('#side-menu').metisMenu();
            $('[data-toggle="tooltip"]').tooltip();

            // check All
            $('#all-checkbox').click(function(){
                if( $(this).prop('checked') )
                    $('td input[type="checkbox"]').prop('checked', true);
                else
                    $('td input[type="checkbox"]').prop('checked', false);
            });

            // 批量删除按钮
            $('#btn-removeAll').on('click', function(){

            });
        })

        // 显示添加模态框
        function addModal(self)
        {
            $('#url').val('');
            $('#addModal').modal('show');
        }

        // 添加URL到白名单
        function addUrl()
        {
            var host = $("#host").val();
            $.post('/config/white/add', {host: host}).done(function(data){
                console.log('white add: ' + host);
                if (data == 'success'){
                    var params = {
                        url:host,
                        status:1
                    };

                    addItem( params );
                    $('#addModal').modal('hide');
                    $('.notList').remove();
                }else {
                    alert('failed');
                }
            });
            
        }

        // 添加到列表
        function addItem( params )
        {
            if( typeof params == 'object' )
            {
                var tr = [];
                tr.push('<tr>');
                tr.push( '<td class="text-center"><input type="checkbox"></td>' );
                tr.push( '<td><a href="' + params.url + '" data-toggle="tooltip" data-placement="bottom" title="' + params.url + '" target="_black">' + params.url + '</a></td>' );
                
                if( params.status == 1 )
                    tr.push( '<td class="text-center"><i class="status status-success" data-toggle="tooltip" data-placement="right" title="Start" onclick="reStart($(this), 0)"></i></td>' );
                else
                    tr.push( '<td class="text-center"><i class="status status-error" data-toggle="tooltip" data-placement="right" title="Error" onclick="reStart($(this), 1)"></i></td>' );

                tr.push( '<td class="wmax80"><a href="javascript:;" class="btn-xs btn-link btn-delete" onclick="removeItem($(this))">Delete</a></td>' );
				tr.push( '</tr>' );


                $('.table-trust tbody').append( tr.join('') );
            }
            else
            {
                $('.table-trust tbody').append( params );
            }
        }

        // 修改白名单状态
        function reStart(self, status)
        {
            $.ajax({
                method: 'post',
                url: '',
                data:{ status:status },
                success: function(res)
                {
                    console.log('success');
                }
            });
        }

        // 移除白名单
        function removeItem(self)
        {   
            var host = self.parent().parent().children('td').children('input').val();
            $.post('/config/white/del', {host: host}).done(function(data){
                if (data == 'success') {
                    self.parent().parent().remove();
                    var number = $('.table-trust tbody tr').length;
                    number == 0 && addItem( '<tr class="notList"><td colspan="5" class="text-center">Not Found List</td></tr>' );
                }
            });
            
        }
    </script>
</body>
</html>
{% end %}